package appapi.controller.oider;

import appapi.Request.*;
import appapi.Vo.AddModuleVo;
import appapi.Vo.EditModuleVo;
import appapi.common.ResponseBean;
import appapi.entity.auth.ModulelistEntity;
import appapi.entity.auth.ModuletypeModel;
import appapi.service.ModulelistService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模块信息
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:28
 */
@Api(tags = "模块信息")
@RestController
@RequestMapping("/api/modulelist")
public class ModulelistController {

    private static final Logger logger = LoggerFactory.getLogger(ModulelistController.class);
    @Autowired
    private ModulelistService modulelistService;

    /**
     * 分页列表
     */
    @GetMapping("/getModuleListByPage")
    @ApiOperation(value = "模块信息分页列表")
    public ResponseBean<ModulelistEntity> GetPageList(@Valid  ModulelistPageReq modulelistPageReq){
        try {
            if (modulelistPageReq.getCurrPage() == null || modulelistPageReq.getPageSize() == null) {
                modulelistPageReq.setCurrPage(1);
                modulelistPageReq.setPageSize(10);
            }
            PageInfo<ModulelistEntity> pageInfo = modulelistService.page(modulelistPageReq);
            return ResponseBean.success(pageInfo);
        } catch (Exception e) {
            logger.error("分页获取模块信息异常", e);
            return ResponseBean.fail("分页获取模块信息异常");
        }

    }

    /**
     * 选择列表
     */
    @GetMapping("/getModuleList")
    @ApiOperation(value = "获取模块列表")
    public ResponseBean<ModulelistEntity> list(@Valid ModulelistReq modulelistReq){
        try {
            List<ModulelistEntity> dataList = modulelistService.list(modulelistReq);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取模块列表异常", e);
            return ResponseBean.fail("获取模块列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getModuleDetail")
    @ApiOperation(value = "模块信息信息")
    public ResponseBean<ModulelistEntity> detail(ModuleDetailReq req) {
        try {
            if (req.getModuId() == null) {
                return ResponseBean.fail("模块ID不能为空");
            }
            ModulelistEntity entity = modulelistService.detail(req.getModuId());
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取详情失败");
            }
        } catch (Exception e) {
            logger.error("获取模块详情异常", e);
            return ResponseBean.fail("获取模块详情异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping(value = "/addModuleList")
    @ApiOperation(value = "新增模块信息")
    public ResponseBean insert(@Validated @RequestBody AddModuleVo moduleVo){
        try {
            ModulelistEntity entity = moduleVo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = modulelistService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("模块标识已存在，不能重复增加");
            }
            int iRet = modulelistService.insert(entity);
            if(iRet>0)
            {
                return ResponseBean.success("保存成功");
            }else{
                return ResponseBean.fail("保存失败");
            }
        }catch (Exception e)
        {
            logger.error("添加模块异常", e);
            return ResponseBean.fail("添加模块异常");
        }
    }
    /**
     * 修改
     */
    @PostMapping(value = "/editModuleList")
    @ApiOperation(value = "修改模块信息")
    public ResponseBean update(@Validated @RequestBody EditModuleVo moduleVo){
        ModulelistEntity entity = moduleVo.vo2Entity();
        try {
            entity.setUpdateTime(LocalDateTime.now());
            int count = modulelistService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("模块标识已存在，不能重复修改");
            }
            int iRet= modulelistService.update(entity);
            if(iRet>0)
            {
                return ResponseBean.success("更新成功");
            }else{
                return ResponseBean.fail("更新失败");
            }
        }catch (Exception e){
            logger.error("更新模块异常", e);
            return ResponseBean.fail("更新模块异常");
        }
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteModuleList")
    @ApiOperation(value = "删除模块信息")
    public ResponseBean delete(@RequestBody DelModuleReq req) {
        try {
            if (req.getModuId() == null) {
                return ResponseBean.fail("模块ID不能为空");
            }
            int iRet = modulelistService.delete(req.getModuId());
            if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        }catch (Exception e){
            logger.error("删除模块异常", e);
            return ResponseBean.fail("删除模块异常");
        }
    }

    @GetMapping("/getTypeAndChildren")
    @ApiOperation(value = "获取授权模块")
    public ResponseBean getTypeAndChildren() {
        try {
            ModulelistReq childReq =new ModulelistReq();
            List<ModulelistEntity> moduleList = modulelistService.list(childReq);
            List<ModuletypeModel> dtoList=new ArrayList<>();
            List<ModulelistEntity>  menuList = moduleList.stream().filter(ModulelistEntity->ModulelistEntity.getParentId()==0)
                    .collect(Collectors.toList());

            for (ModulelistEntity type:menuList
            ) {
                ModuletypeModel dto =new ModuletypeModel();
                dto.setModuId(type.getModuId());
                dto.setModuIcon(type.getModuIcon());
                dto.setModuSort(type.getModuSort());
                dto.setModuNote(type.getModuNote());
                dto.setModuType(type.getModuType());
                dto.setModuName(type.getModuName());
                dto.setModuState(type.getModuState());
                dto.setModuUrl(type.getModuUrl());
                dto.setModuTag(type.getModuTag());
                dto.setParentId(type.getParentId());
                dto.setParentName(type.getParentName());
                List<ModulelistEntity> child=moduleList.stream().filter(ModulelistEntity->ModulelistEntity.getParentId().equals(type.getModuId()))
                        .collect(Collectors.toList());
                dto.setChildren(child);
                dtoList.add(dto);
            }
            return ResponseBean.success(dtoList);
        } catch (Exception e) {
            logger.error("获取模块权限列表异常", e);
            return ResponseBean.fail("获取模块权限列表异常");
        }
    }
}
